<?php 
class annonce_region extends MX_Controller{
	public $category;
	public $page;
	public function index($region='',$vPage='',$page=''){
		
		$title=' gratuites ';
		$description='Consultez ';
		$message_tous =0;
		$message_particuliers =0 ;
		$message_professionnels =0;
		$nom_region='';
		$TopAnnonces=$annonces=array();
		if($region){
			$regionObject=$this->model()->get_region($region);
			$title.=$regionObject->nom_region .' caverne.fr';
			
			
			if($regionObject->code_departement){
				
				//set session region
				$this->load->library('session');
				$this->session->set_userdata('search_region',$regionObject->code_departement);
				
				$chercher_annonce=$this->load->Module('annonce/chercher');
				$chercher_annonce->load_session();
				$chercher_annonce->region=$regionObject->code_departement; 
				$chercher_annonce->category=$this->session->userdata('category_id');
				$chercher_annonce->page=$page;
				$ids_anonnces=$chercher_annonce->get_list_annonces();
				
				$message_tous =$chercher_annonce->nb;
				$message_particuliers=$chercher_annonce->nb_particuliers;
				$message_professionnels =$chercher_annonce->nb_professionnels;
				$nameCategory=$this->model()->get_name_category(true);
				$description.=$message_tous." annonces $nameCategory gratuites {$regionObject->nom_region} sur caverne.fr ";
				
				
				$product=$this->load->Module('product/show');
				if(!empty($ids_anonnces))
					foreach ($ids_anonnces as $id_annonce){
						$annonces[$id_annonce]=$product->get_info($id_annonce);
					}
				
			}
			$nom_region=$regionObject->nom_region;
		}
		$keywords='annonces '.$nom_region;
		$message_tous.=' annonces';
		$message_particuliers.=' annonces' ;
		$message_professionnels.=' annonces';
		
		$data=array(
				'title'=>$message_tous.' '.$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'paging'=>$chercher_annonce->paging,
				'nb'=>$chercher_annonce->nb,
				'nb_particulier'=>$chercher_annonce->nb_particuliers,
				'nb_professionnells'=>$chercher_annonce->nb_professionnels,
				'message_tous'=>$message_tous,
				'message_particuliers'=>$message_particuliers ,
				'message_professionnels'=>$message_professionnels,
				'annonces'=>$annonces,
				'TopAnnonces'=>$TopAnnonces
		);
		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		 
		$this->view(
				array('header',
					'category',
					'footer'), 
				$data); // Display the page 
	}
	

	private function copy_Photo($path,$cat,$photos=array()){
		if(!empty($photos))
		foreach ($photos as $photo){
			$temp_photo=$path.$photo;
			$temp_thumb_photo=$path."thumbnails/".$photo;
			$NameCatPhoto=$cat."/$photo";
	
			if(!is_dir($path.$cat))mkdir($path.$cat, 0777);
			if(!is_dir($path."thumbnails/".$cat))mkdir($path."thumbnails/".$cat, 0777);
	
			if (@copy($temp_photo,"$path/$NameCatPhoto")) unlink($temp_photo);
			if (@copy($temp_thumb_photo,$path."thumbnails/$NameCatPhoto")) unlink($temp_thumb_photo);
	
			$product_photo[]=$NameCatPhoto;
		}
		if(isset($product_photo)) return $product_photo;
	
		return false;
	}
}